// Pipit C标准库 - IO模块实现
#include "pipit_stdlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#else
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#endif

// 打开文件
FILE* pipit_fopen(const char* filename, const char* mode) {
    return fopen(filename, mode);
}

// 关闭文件
int pipit_fclose(FILE* stream) {
    return fclose(stream);
}

// 读取文件
size_t pipit_fread(void* ptr, size_t size, size_t nmemb, FILE* stream) {
    return fread(ptr, size, nmemb, stream);
}

// 写入文件
size_t pipit_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream) {
    return fwrite(ptr, size, nmemb, stream);
}

// 检查文件是否到达末尾
int pipit_feof(FILE* stream) {
    return feof(stream);
}

// 获取文件大小
long pipit_file_size(FILE* stream) {
    long current = ftell(stream);
    fseek(stream, 0, SEEK_END);
    long size = ftell(stream);
    fseek(stream, current, SEEK_SET);
    return size;
}

// 读取整个文件内容到字符串
char* pipit_read_file(const char* filename) {
    FILE* file = fopen(filename, "rb");
    if (!file) return NULL;
    
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
    char* content = malloc(size + 1);
    if (!content) {
        fclose(file);
        return NULL;
    }
    
    fread(content, 1, size, file);
    content[size] = '\0';
    
    fclose(file);
    return content;
}

// 将字符串写入文件
int pipit_write_file(const char* filename, const char* content) {
    FILE* file = fopen(filename, "wb");
    if (!file) return -1;
    
    size_t len = strlen(content);
    size_t written = fwrite(content, 1, len, file);
    
    fclose(file);
    return (written == len) ? 0 : -1;
}

// 检查文件是否存在
int pipit_file_exists(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file) {
        fclose(file);
        return 1;
    }
    return 0;
}

// 删除文件
int pipit_remove_file(const char* filename) {
    return remove(filename);
}

// 重命名文件
int pipit_rename_file(const char* oldname, const char* newname) {
    return rename(oldname, newname);
}

// 获取当前工作目录
char* pipit_getcwd() {
    char* buffer = malloc(1024);
    if (getcwd(buffer, 1024)) {
        return buffer;
    }
    free(buffer);
    return NULL;
}

// 创建目录
int pipit_mkdir(const char* path) {
#ifdef _WIN32
    return _mkdir(path);
#else
    return mkdir(path, 0755);
#endif
}

// 删除目录
int pipit_rmdir(const char* path) {
    return rmdir(path);
}

// 获取文件信息
int pipit_get_file_info(const char* filename, pipit_file_info_t* info) {
    struct stat st;
    if (stat(filename, &st) != 0) {
        return -1;
    }
    
    info->size = st.st_size;
    info->is_directory = S_ISDIR(st.st_mode);
    info->is_regular_file = S_ISREG(st.st_mode);
    info->last_modified = st.st_mtime;
    
    return 0;
}

// 读取文件到缓冲区
int pipit_read_buffer(const char* filename, void* buffer, size_t buffer_size) {
    FILE* file = fopen(filename, "rb");
    if (!file) return -1;
    
    size_t bytes_read = fread(buffer, 1, buffer_size, file);
    fclose(file);
    
    return bytes_read;
}

// 从缓冲区写入文件
int pipit_write_buffer(const char* filename, const void* buffer, size_t buffer_size) {
    FILE* file = fopen(filename, "wb");
    if (!file) return -1;
    
    size_t written = fwrite(buffer, 1, buffer_size, file);
    fclose(file);
    
    return (written == buffer_size) ? 0 : -1;
}

// 逐行读取文件
pipit_line_reader_t* pipit_line_reader_open(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (!file) return NULL;
    
    pipit_line_reader_t* reader = malloc(sizeof(pipit_line_reader_t));
    reader->file = file;
    return reader;
}

char* pipit_line_reader_next(pipit_line_reader_t* reader) {
    if (!reader || !reader->file) return NULL;
    
    if (fgets(reader->buffer, sizeof(reader->buffer), reader->file)) {
        // 移除换行符
        size_t len = strlen(reader->buffer);
        if (len > 0 && (reader->buffer[len-1] == '\n' || reader->buffer[len-1] == '\r')) {
            reader->buffer[len-1] = '\0';
        }
        return reader->buffer;
    }
    return NULL;
}

void pipit_line_reader_close(pipit_line_reader_t* reader) {
    if (reader) {
        if (reader->file) {
            fclose(reader->file);
        }
        free(reader);
    }
}